// pages/view/ai-view/recommend-detail/recommend.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    manImages: [
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/1.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/2.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/3.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/4.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/5.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/6.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/7.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/8.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/9.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/man/10.jpg'
      }
    ],
    womenImages: [
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/1.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/2.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/3.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/4.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/5.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/6.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/7.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/8.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/9.jpg'
      },
      {
        url: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/face-ai/clothing/women/10.jpg'
      }
    ],
    showImages: [],
    gender: '1'
  },

  //从一个给定的数组arr中,随机返回num个不重复项
  getArrayItems(arr, num) {
    //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
    var temp_array = new Array();
    for (var index in arr) {
        temp_array.push(arr[index]);
    }
    //取出的数值项,保存在此数组
    var return_array = new Array();
    for (var i = 0; i<num; i++) {
        //判断如果数组还有可以取出的元素,以防下标越界
        if (temp_array.length>0) {
            //在数组中产生一个随机索引
            var arrIndex = Math.floor(Math.random()*temp_array.length);
            //将此随机索引的对应的数组元素值复制出来
            return_array[i] = temp_array[arrIndex];
            //然后删掉此索引的数组元素,这时候temp_array变为新的数组
            temp_array.splice(arrIndex, 1);
        } else {
            //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
            break;
        }
    }
    console.log(return_array)
    return return_array;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options:', options)
    this.setData({
      gender: options.gender
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const showImage = this.getArrayItems(this.data.gender === '1' ? this.data.manImages : this.data.womenImages, 4)
    this.setData({
      showImages: showImage
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})